﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.PersonalInfo
{
    public partial class WorkInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
 

            string strSave = Request.Form["hidSave"];
            if (strSave == "Save")
            {
                string strhidCom_0 = Request.Form["hidCom_0"];
                string strhidCom_1 = Request.Form["hidCom_1"];
                string strhidCom_2 = Request.Form["hidCom_2"];
                string strhidCom_3 = Request.Form["hidCom_3"];
                string strhidCom_4 = Request.Form["hidCom_4"];
                //处理工作信息
                CareerX(strhidCom_0, userID, "0");
                CareerX(strhidCom_1, userID, "1");
                CareerX(strhidCom_2, userID, "2");
                CareerX(strhidCom_3, userID, "3");
                CareerX(strhidCom_4, userID, "4");

                //加入动态
                Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                mUserForState = bllUserForState.GetModel(userID);
                new Jy123.BLL.Public.State().CreateModifyUserState(mUserForState, null, (int)Jy123.BLL.Public.EnumConfig.StateActionType.ModifyUserInfo);
                //加入动态
            }
            StringBuilder sbHtml = new StringBuilder(Cache["user_workInfo"].ToString());//最终写出页面的大sb
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            ShowPageData(sbHtml, userID);//载入页面数据
            Response.Write(sbHtml.ToString());
        }
        /// <summary>
        /// 载入页面数据
        /// </summary>
        /// <param name="sbHtml">sb</param>
        /// <param name="userID">当前用户ID</param>
        /// <returns>sb</returns>
        private StringBuilder ShowPageData(StringBuilder sbHtml, int userID)
        {
            List<Jy123.Model.User_Career> listUserCareer = new Jy123.BLL.User_Career().GetModelList("Fuser='" + userID + "' order by joinyear desc ,joinmonth desc,ID DESC");
            for (int i = 0; i < 5; i++)
            {
                if (i <= listUserCareer.Count - 1)
                {
                    //sbHtml = sbHtml.Replace("$CareerID_" + i.ToString() + "$",listUserCareer[i].ID.ToString());//ID
                    sbHtml = sbHtml.Replace("$Classhid_" + i.ToString() + "$", string.Empty);//显示UL
                    sbHtml = sbHtml.Replace("$ComName_" + i.ToString() + "$", listUserCareer[i].Company);//公司名称
                    sbHtml = sbHtml.Replace("$ComDescription_" + i.ToString() + "$", listUserCareer[i].Description);//公司描述
                    sbHtml = sbHtml.Replace("$FromYear_" + i.ToString() + "$", listUserCareer[i].JoinYear.ToString());//入职年份
                    sbHtml = sbHtml.Replace("$FromMonth_" + i.ToString() + "$", listUserCareer[i].JoinMonth.ToString());//入职月份
                    sbHtml = sbHtml.Replace("$hidValue_" + i.ToString() + "$", listUserCareer[i].ID.ToString());//修改隐藏域的值为记录的ID
                    if (listUserCareer[i].IsStill)
                    {
                        sbHtml = sbHtml.Replace("$checked_" + i.ToString() + "$", " checked='checked'");//是否到现在
                        sbHtml = sbHtml.Replace("$hid_" + i.ToString() + "$", "hid");//到现在，隐藏离职时间
                        sbHtml = sbHtml.Replace("$ToYear_" + i.ToString() + "$", "0");//离职年份
                        sbHtml = sbHtml.Replace("$ToMonth_" + i.ToString() + "$", "0");//离职月份

                    }
                    else
                    {
                        sbHtml = sbHtml.Replace("$checked_" + i.ToString() + "$", string.Empty);
                        sbHtml = sbHtml.Replace("$hid_" + i.ToString() + "$", string.Empty);//到现在，隐藏离职时间
                        sbHtml = sbHtml.Replace("$ToYear_" + i.ToString() + "$", listUserCareer[i].LeaveYear.ToString());
                        sbHtml = sbHtml.Replace("$ToMonth_" + i.ToString() + "$", listUserCareer[i].LeaveMonth.ToString());
                    }
                }
                else
                {
                    sbHtml = sbHtml.Replace("$Classhid_" + i.ToString() + "$", "hid");//隐藏多余的UL
                    sbHtml = sbHtml.Replace("$ComName_" + i.ToString() + "$", string.Empty);//公司名称
                    sbHtml = sbHtml.Replace("$ComDescription_" + i.ToString() + "$", string.Empty);//公司描述
                    sbHtml = sbHtml.Replace("$FromYear_" + i.ToString() + "$", "0");//入职年份
                    sbHtml = sbHtml.Replace("$FromMonth_" + i.ToString() + "$", "0");//入职月份
                    sbHtml = sbHtml.Replace("$hidValue_" + i.ToString() + "$", "show");//修改隐藏域的值为记录的ID--0

                    sbHtml = sbHtml.Replace("$checked_" + i.ToString() + "$", " checked='checked'");
                    sbHtml = sbHtml.Replace("$hid_" + i.ToString() + "$", "hid");//到现在，隐藏离职时间
                    sbHtml = sbHtml.Replace("$ToYear_" + i.ToString() + "$", "0");
                    sbHtml = sbHtml.Replace("$ToMonth_" + i.ToString() + "$", "0");

                    sbHtml = sbHtml.Replace("$hidValue_" + i.ToString() + "$", "hide");//修改隐藏域的值为0
                }
            }
            return sbHtml;
        }

        /// <summary>
        /// 处理工作信息x
        /// </summary>
        /// <param name="strhidCom_X"></param>
        /// <param name="userID">用户ID</param>
        /// <param name="mark">第几个工作信息(0~4)</param>
        private void CareerX(string strhidCom_X, int userID, string mark)
        {
            //第x个工作信息
            if (strhidCom_X != "hide" && strhidCom_X != string.Empty)
            {
                Jy123.Model.User_Career mCareer = new Jy123.Model.User_Career();
                Jy123.BLL.User_Career bllCareer = new Jy123.BLL.User_Career();
                //hidValue_
                //string strmark = Request.Form["careerID_"+mark];
                //string strcareerID = Request.Form["careerID_" + mark];
                string strcareerID = Request.Form["hidCom_" + mark];
                string strcompany = Request.Form["company_" + mark];
                string strcomDescription = Request.Form["comDescription_" + mark];
                string strcomFromYear = Request.Form["comFromYear_" + mark];
                string strcomFromMonth = Request.Form["comFromMonth_" + mark];
                string strchkAtNow = Request.Form["chkAtNow_" + mark];
                string strcomToYear = Request.Form["comToYear_" + mark];
                string strcomToMonth = Request.Form["comToMonth_" + mark];

                switch (strhidCom_X)
                {
                    case "show"://插入新纪录
                        if (userID > 0 && strcompany != "")
                        {
                            mCareer.FUser = userID;
                            mCareer.Company = strcompany;
                            mCareer.Description = strcomDescription;
                            mCareer.JoinYear = Convert.ToInt32(strcomFromYear);
                            mCareer.JoinMonth = Convert.ToInt32(strcomFromMonth);
                            if (strchkAtNow == "on")//工作到现在
                            {
                                mCareer.IsStill = true;
                                mCareer.LeaveYear = 0;
                                mCareer.LeaveMonth = 0;
                            }
                            else
                            {
                                mCareer.LeaveYear = Convert.ToInt32(strcomToYear);
                                mCareer.LeaveMonth = Convert.ToInt32(strcomToMonth);
                            }
                            mCareer.PostTime = DateTime.Now;
                            mCareer.PostIP = Common.Public.GetClientIP();
                            bllCareer.Add(mCareer);
                        }
                        else
                        { /*为空时的处理*/};
                        break;
                    default://更新原记录
                        mCareer = bllCareer.GetModel(Convert.ToInt32(strcareerID));
                        if (mCareer.Company.Trim() != "")
                        {
                            
                            mCareer.Company = strcompany;
                            mCareer.Description = strcomDescription;
                            mCareer.JoinYear = Convert.ToInt32(strcomFromYear);
                            mCareer.JoinMonth = Convert.ToInt32(strcomFromMonth);
                            if (strchkAtNow == "on")//工作到现在
                            {
                                mCareer.IsStill = true;
                                mCareer.LeaveYear = 0;
                                mCareer.LeaveMonth = 0;
                            }
                            else
                            {
                                mCareer.LeaveYear = Convert.ToInt32(strcomToYear);
                                mCareer.LeaveMonth = Convert.ToInt32(strcomToMonth);
                            }

                            bllCareer.Update(mCareer);
                        }
                        else
                        { /*为空时的处理*/};
                        break;
                }
            }
        }
    }
}
